/*学校管理*/
layui.define(['layer','form'], function(exports) {
    var $ = layui.jquery;
    var layer=layui.layer;
    var form = layui.form();
    
    form.on('checkbox(check)', function(data){
        if(data.elem.checked){
            $("input[name='checkbox']").prop("checked", true);
        }else{
            $("input[name='checkbox']").prop("checked", false);
        }  
        form.render('checkbox');      
    }); 
    
    $.post("/srv/rdo.php",{"tpl":"get_period_list2"},function(d){
    	var d=eval('('+d+')');
    	var html = "<option value=''>选择学段</option>";
    	var d2=[];
    	if(d){
    		for(var i=0;i<d.length;i++){
				html+='<option value='+d[i]['id']+'>'+d[i]['name']+'</option>';
				d2[d[i]['id']]=d[i]['name'];
			}
    	}
        $("#period").html(html);
        $("#period").val($("#period").attr("period"));
        form.render('select');
        
        $(".period_id").each(function(){
        	var period_id=$(this).text();
        	if(period_id!=''){
        		period_id=period_id.split(",");
        		var val='';
        		for(var i=0;i<period_id.length;i++){
        			val+=d2[period_id[i]]+',';
        		}
        		$(this).text(val);
        	}
        })
    })
    $.post("/srv/rdo.php",{"tpl":"get_mechanism_list"},function(d){
    	var html = "<option value=''>选择机构</option>"+d;
        $("#mechanism").html(html);
        $("#mechanism").val($("#mechanism").attr("mechanism"));
        form.render('select');
    })
    $.post("./srv/rdo.php",{"tpl":"get_dict_list"},function(d){
    	var html = "<option value='' childs=''>选择地区</option>"+d;
        $("#dict").html(html);
        $("#dict").val($("#dict").attr("dict"));
        form.render('select');
    })
    
    $("#btn_add").click(function(){
        add_dict();
     });
     
     function add_dict() {
        //iframe层
        layer.open({
            type: 2,
            title: '添加学校',
            shadeClose: false,
            shade: 0.8,
            area: ['100%', '100%'],
            full:true,
            content: '?t=system_edit_school', //iframe的url
            end: function(){ 
                location.reload();
            }
        }); 
    }
    $("#btn_del").click(function(){
        var m_val="";
        $("[name='checkbox']:checked").each(function(i, obj) {
			m_val+=$(this).val()+",";
		});
        if(m_val.lastIndexOf(",")>0){
            m_val=m_val.substring(0,m_val.length-1) ;
        }
        if(m_val.length>0){ 
            layer.confirm('确定删除？',function(index){
                $.post("/srv/sdo.php", {tpl:"del_school_more",id:m_val}, function (d, e) {  
                    if(d=="ok"){
                        location.reload();
                    }
                });
            });
        }else{
            layer.msg('没有选择数据');
        } 
    });
    $('button[data-opt=update]').click(function(){
        var t_id=$(this).data('id');
        edit_dict(t_id);
    });
    function edit_dict(id) {
        //iframe层
        layer.open({
            type: 2,
            title: '修改学校',
            shadeClose: false,
            shade: 0.8,
            area: ['100%', '100%'],
            full:true,
            content: '?t=system_edit_school&id='+id, //iframe的url
            end: function(){ 
                location.reload();
            }
        }); 
    }
    $('button[data-opt=del]').click(function(){
        var id=$(this).data('id');
        layer.confirm('确定删除？',function(index){
            $.post("/srv/sdo.php", {tpl:"del_school_single",id:id}, function (d, e) {  
                if(d=="ok"){
                    location.reload();
                }
            });
        });
    });
    $('button[data-opt=check]').click(function(){
        var id=$(this).data('id');
        var par_isc=$(this).parent().prev().find("par_isc");
        var ischeck=par_isc.attr("v1");
        $.post("/srv/sdo.php", {tpl:"check_school",id:id}, function (d, e) {  
            if(d=="ok"){
            	ischeck=1-ischeck;
            	$text=(ischeck==1)?'<i class="iconfont">&#xe61d;</i>':'<i class="iconfont">&#xe6a1;</i>';
            	par_isc.attr("v1",ischeck);
                par_isc.html($text);
            }
        });
    });

    $("#btn_ref").click(function(){
        location.reload();
    });
    
    exports('system_school', {});
    
});